/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.DAO;

import desu.db.HibernateUtil;
import desu.nya.Anime;
import desu.nya.Genre;
import desu.nya.GenreList;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

/**
 *
 * @author Fess
 */
public class GenreListIMPL implements GenreListDAO{
    
    @Override
    public void addGenreList(GenreList genreList) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(genreList);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при вставке жанра");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public GenreList getGenreByID(Long id) throws SQLException {
//        session=null;
//        try{
//            session=HibernateUtil.getSessionFactory().openSession();
//            
//        }
//        catch(Exception ex){
//            System.err.println("Ошибка при вставке жанра");
//        }
//        finally{
//            if(session!=null&&session.isOpen())
//                session.close();
//        }
        return null;
    }

    @Override
    public void updateGenre(Long genreList_id, GenreList genreList) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<String> getAllGenreByAnime(Long animeId) throws SQLException {
        Session session=null;
//        List<Genre> list=new ArrayList<Genre>();
        List<String> list=null;
        String hql="select gl.genreId.genreName from GenreList gl where gl.animeId=:aId";
        try{
//            List<GenreList> tList;
            session=HibernateUtil.getSessionFactory().openSession();
            Query query=session.createQuery(hql);
            query.setLong("aId", animeId);
            list=query.list();
            System.out.println("HiB: geAllAnimeById() get ID: "+animeId);
//            Criteria cr=session.createCriteria(GenreList.class);
//            tList=cr.add(Expression.eq("animeId",anime)).list();
//            for(GenreList gl:tList){
//                list.add(gl.getGenreId());
//            }
//            Criteria cr2=cr.createCriteria("Genre");
            System.out.println("Genre found successful");
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.err.println("Genre found error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return list;
    }

    @Override
    public void deleteGenre(GenreList genreList) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void appendGenreListToAnime(Anime anime, List<Long> genres) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Genre genre;
            GenreList gl;
            for(int i=0;i<genres.size();i++){
                genre=(Genre)session.load(Genre.class, genres.get(i));
                gl=new GenreList(genre, anime);
                session.save(gl);
            }
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Append genres to anime error");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public void addGenreListFromList(List<Genre> genreList) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
//            session.beginTransaction();
//            session.save(genreList);
//            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при вставке жанра");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }
    
}
